package com.appos.service;

import com.appos.domain.Book;
import com.appos.domain.WorkSheet;
import com.appos.domain.WorkSheetGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkSheetGroupService {
    private static final Logger logger = LoggerFactory.getLogger(WorkSheetGroupService.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存对象
     * @param workSheetGroup
     * @return
     */
    public int save(WorkSheetGroup workSheetGroup) {
        workSheetGroup.setCreateTime(new Date());
        workSheetGroup.setUpdateTime(new Date());
        mongoTemplate.save(workSheetGroup);
        return 1;
    }

    /**
     * 查询所有
     * @return
     */
    public List<WorkSheetGroup> findAll() {
        return mongoTemplate.findAll(WorkSheetGroup.class);
    }

    /***
     * 根据workSheetGroupUuid查询
     * @param ids
     * @return
     */
    public List getWorkSheetGroupList(List ids) {
        Query query = new Query(Criteria.where("workSheetGroupUuid").in(ids));
        return mongoTemplate.find(query, WorkSheetGroup.class);
    }

    /**
     * 更新对象
     *
     * @param workSheetGroup
     * @return
     */
    public int update(WorkSheetGroup workSheetGroup) {
        Query query = new Query(Criteria.where("workSheetGroupUuid").is(workSheetGroup.getWorkSheetGroupUuid()));
        Update update = new Update().set("workSheetGroupName", workSheetGroup.getWorkSheetGroupName()).set("updateTime",
                new Date());
        mongoTemplate.updateFirst(query, update, WorkSheetGroup.class);

        return 1;
    }
}
